﻿using Autofac;
using Autofac.Integration.Mvc;
using liaowa2.Dao.Manager;
using liaowa2.Dao.Web;
using liaowa2.Impl.Manager;
using liaowa2.Interface.Manager;
using liaowa2.Web.App_Start;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace liaowa2
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {

            var builder = new ContainerBuilder();
            SetupResolveRules(builder);
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            //log4
            XmlConfigurator.ConfigureAndWatch(new FileInfo(Server.MapPath("/Configs/Log4Net.config")));
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            
        }


        /// <summary>
        /// autofac手动注入
        /// </summary>
        /// <param name="builder"></param>
        private void SetupResolveRules(ContainerBuilder builder)
        {
            builder.RegisterType<ArticleDao>();
            builder.RegisterType<ArticleImpl>().As<IArticleInterface>();
            builder.RegisterType<ManagerRoleImpl>().As<IManagerRoleInterface>();
            builder.RegisterType<ManagerUserImpl>().As<IManagerUserInterface>();
            builder.RegisterType<NavigationImpl>().As<INavigationInterface>();
            builder.RegisterType<ManagerRoleValueImpl>().As<IManagerRoleValueInterface>();
        }
    }
}
